package com.itheima.reggie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <b><code>SetmealServiceImpl</code></b>
 * <p/>
 * Description
 * <p/>
 * <b>Creation Time:</b> 24/06/2024 16:42.
 *
 * @author yanjr
 * @since reggie_take_out
 */
@Service("setmealService")
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询套餐信息
     * @param page 当前页
     * @param pageSize 每页显示条数
     * @param name 套餐名称
     * @return 分页查询结果
     */
    @Override
    public R<Page<Setmeal>> querySetmeal(int page, int pageSize, String name) {
        // 分页信息
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        // 查询条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<Setmeal>().like(StrUtil.isNotEmpty(name), Setmeal::getName, name).orderByDesc(Setmeal::getUpdateTime);
        Page<Setmeal> result = baseMapper.selectPage(pageInfo, queryWrapper);
        return R.success(result);
    }

    /**
     * 根据id查询套餐信息
     * @param id 套餐id
     * @return 套餐信息
     */
    @Override
    public R<SetmealDto> queryById(Long id) {
        // 套餐信息
        Setmeal setmeal = this.baseMapper.selectById(id);
        if (setmeal != null) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtil.copyProperties(setmeal, setmealDto);
            List<SetmealDish> setmealDishes = setmealDishMapper.selectList(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, id));
            setmealDto.setSetmealDishes(setmealDishes);
            return R.success(setmealDto);
        }
        return null;
    }

    /**
     * 更新套餐信息
     * @param dto 套餐信息
     * @return 是否成功
     */
    @Override
    public R<String> updateSetmeal(SetmealDto dto) {
        // 删除菜品
        this.delteDishByIds(dto.getId().toString());
        // 新增菜品
        this.addSetmeal(dto);
        return R.success("操作成功");
    }

    /**
     * 批量删除
     * @param ids 菜品id，多个以逗号分隔
     * @return 操作是否成功
     */
    @Override
    public R<String> delteDishByIds(String ids) {
        ArrayList<String> idList = CollectionUtil.toList(ids.split(","));
        // 先套餐菜品
        setmealDishMapper.delete(new LambdaQueryWrapper<SetmealDish>().in(SetmealDish::getSetmealId, idList));
        // 再删除套餐
        baseMapper.deleteBatchIds(idList);
        return R.success("删除成功");
    }

    /**
     * 新增套餐
     * @param dto 套餐信息
     * @return 是否新增成功
     */
    @Override
    public R<String> addSetmeal(SetmealDto dto) {
        // 新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtil.copyProperties(dto, setmeal);
        save(setmeal);
        // 新增套餐菜品
        if (CollectionUtil.isNotEmpty(dto.getSetmealDishes())) {
            for (SetmealDish setmealDish : dto.getSetmealDishes()) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDish.setId(null);
                setmealDishMapper.insert(setmealDish);
            }
        }
        return R.success("添加成功");
    }

    /**
     * 批量起售/停售
     * @param status 状态，起售-1，停售-0
     * @param ids 菜品id，多个以逗号分隔
     * @return 操作是否成功
     */
    @Override
    public R<String> changeStatus(Integer status, String ids) {
        String[] idArray = ids.split(",");
        ArrayList<Setmeal> setmeals = new ArrayList<>();
        for (String s : idArray) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(new Long(s));
            setmeal.setStatus(status);
            setmeals.add(setmeal);
        }
        this.updateBatchById(setmeals);
        return R.success("操作成功");
    }
}
